//
//  TrainingRecordsTableViewCell.swift
//  DaRings
//
//  Created by 魔样科技 on 2022/11/5.
//

import Foundation
import MYModule
import UIKit

open class TrainingRecordsTableViewCell: TableViewCell {
    
    public let dateLabel = Label()
    
    open override func makeUI() {
        super.makeUI()
        
        self.contentView.backgroundColor = UIColor.background
        
        self.durationView.iconImageView.image = UIImage(named: "ic_activity_workout_records_duration")
        self.durationView.numLabel.text = "--"
        self.durationView.unitLabel.text = "unit_minute".language
        
        self.kcalView.iconImageView.image = UIImage(named: "ic_workout_popular_calories")
        self.kcalView.numLabel.text = "--"
        self.kcalView.unitLabel.text = "unit_calorie".language
        
        self.heartRateView.iconImageView.image = UIImage(named: "ic_workout_popular_hr")
        self.heartRateView.numLabel.text = "--"
        self.heartRateView.unitLabel.text = "gps_training_bpm".language
        
        self.arrowImageView.image = UIImage(named: "ic_continue")?.originalImageWithColor(color: UIColor.assist_D0D0D0)
        
        self.dateLabel.font = Fonts.systemFont(ofSize: 10)
        self.dateLabel.textColor = UIColor.assist_D0D0D0
        
        contentView.addSubview(dateLabel)
        self.contentView.addSubview(self.iconImageView)
        self.contentView.addSubview(self.arrowImageView)
        self.contentView.addSubview(self.titleLabel)
        self.contentView.addSubview(self.durationView)
        self.contentView.addSubview(self.kcalView)
        self.contentView.addSubview(self.heartRateView)
        
        dateLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(arrowImageView.snp.left).offset(-10)
        }
        
        iconImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(20)
            make.top.equalToSuperview().offset(18.5)
        }
        
        
       arrowImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(-20)
        }
        
        
       titleLabel.snp.makeConstraints { make in
            make.bottom.equalTo(self.contentView.snp.centerY)
            make.top.equalTo(16)
            make.trailing.lessThanOrEqualTo(self.arrowImageView.snp.leading).offset(-20)
            make.leading.equalTo(self.iconImageView.snp.trailing).offset(4)
        }
        
        
        durationView.snp.makeConstraints { make in
            make.top.equalTo(self.titleLabel.snp.bottom).offset(3)
            make.leading.equalTo(self.titleLabel.snp.leading)
            make.bottom.equalTo(-16)
        }
        
        kcalView.snp.makeConstraints { make in
            make.centerY.equalTo(self.durationView.snp.centerY)
            make.leading.equalTo(self.durationView.snp.trailing).offset(7)
        }
        
       
        heartRateView.snp.makeConstraints { make in
            make.centerY.equalTo(self.durationView.snp.centerY)
            make.leading.equalTo(self.kcalView.snp.trailing).offset(7)
        }
    }
    
   
    open func bind(_ model: TrainingModel) {
        
        guard let type = TrainingType(rawValue: model.type) else { return }
        
        let date = Date(seconds: TimeInterval(model.startTime))
        let dateStr = date.toString(.custom("HH: mm"))
        dateLabel.text = dateStr
        
        var hour = 0
        var sec = 0
        var minute = 0
        
        if model.vaildTime > 0 {
            hour = model.vaildTime / 3600
            sec = model.vaildTime % 3600
            minute = Int(ceil(Double(sec) / 60.0)) +  hour * 60
        } else {
            if model.endTime - model.startTime >= 60 {
                let totalTime = model.endTime - model.startTime
                hour = totalTime / 3600
                sec = totalTime % 3600
                minute = Int(ceil(Double(sec) / 60.0)) +  hour * 60
            } else {
                hour = 0
                sec = 0
                minute = 0
            }
        }
        
        self.titleLabel.text = type.title
        self.iconImageView.image = UIImage(named: type.iconName)
        
        self.durationView.numLabel.text = "\(minute)"
        self.kcalView.numLabel.text = "\(model.kcal)"
        self.heartRateView.numLabel.text = "\(model.heartRateAvg)"
       
    }
    
    
    //MARK: - lazy load -
    
    public lazy var iconImageView: ImageView = {
        let imageView = ImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    public lazy var titleLabel: Label = {
        let label = Label()
        label.font = Fonts.boldSystemFont(ofSize: 14)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    public lazy var durationView: TrainingRecordItemStackView = {
        let view = TrainingRecordItemStackView()
        return view
    }()
    
    public lazy var kcalView: TrainingRecordItemStackView = {
        let view = TrainingRecordItemStackView()
        return view
    }()
    
    public lazy var heartRateView: TrainingRecordItemStackView = {
        let view = TrainingRecordItemStackView()
        return view
    }()
    
    public lazy var arrowImageView: ImageView = {
        let imageView = ImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
}

